import axios from 'axios'
import { getToken, removeToken } from './auth'
import router from '../router'
import vueMain from '../main'

// const baseURL = 'http://192.168.31.84:5000'
// const baseURL = 'http://172.16.10.59:5000'
const baseURL = 'http://172.16.40.213:5000'
// const baseURL = 'https://wyj.xiaoyou001.top'
// const baseURL = 'http://192.168.31.84:5000'

export const url = baseURL

const request = axios.create({
  baseURL: baseURL,
  timeout: 100000
})

request.interceptors.request.use(
  config => {
    // console.log("请求路径:", config.url);
    if (config.url !== '/login') { config.headers.Authorization = `Bearer ${getToken()}` }
    return config
  }, err => {
    Promise.reject(err)
  })

request.interceptors.response.use(
  config => {
    // console.log(config)

    return config
  }, err => {
    console.log(err.toString())

    if (err.toString() === 'Error: Request failed with status code 401') {
      removeToken()
      vueMain.$notify({
        title: '警告',
        message: '登录状态已失效，即将跳转到登录页',
        type: 'warning'
      })
      setTimeout(() => {
        router.push('/login')
      }, 3000)
      //
      Promise.reject(err).then(res => {
        console.log(res)
      })
    }
  })

export default request
